package com.workspace.service;

import com.workspace.daomain.Customers;
import com.workspace.repository.CustomerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.TimeoutUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class CustomerService {
    @Autowired
    private CustomerRepository customerRepository;
    @Autowired
    private RedisTemplate redisTemplate;
    //登录验证
    public Customers findCustomerByUsername(String username){
        Customers customers=null;
        Object obj=redisTemplate.opsForValue().get("customer_"+username);
        if (obj!=null){
            customers=(Customers) obj;
        }else {
            customers=customerRepository.findCustomersByUsername(username);
            if (customers!=null){
                redisTemplate.opsForValue().set("customer_"+username,customers, 1, TimeUnit.DAYS);
            }
        }
        return customers;
    }
    //列表输出
    public List<Customers> findAllCustomers(){
        List<Customers> customersList=customerRepository.findAllCustomers();
        return customersList;
    }
    //用户删除
    public int deleteCustomer(String sid){
        int status=customerRepository.deleteCustomersBySid(sid);
        return  status;
    }
}
